package com.leejxx.health.mobile.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.Result;
import com.leejxx.health.mobile.vo.OrderSubmitParam;
import com.leejxx.health.pojo.Member;
import com.leejxx.health.pojo.Order;
import com.leejxx.health.pojo.Setmeal;
import com.leejxx.health.service.CheckMealService;
import com.leejxx.health.service.MemberService;
import com.leejxx.health.service.OrderService;
import com.leejxx.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author LeeJx
 * @ClassName OrderController
 * @Description TODO
 * @date 2021/1/6 11:57
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/mobile/order")
public class OrderController {

    @DubboReference
    private SmsService smsService;
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private CheckMealService checkMealService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {
        log.info("[预约服务-预约信息提交]data:{}", orderSubmitParam);
        //验证码校验
        if (!smsService.checkValidateCode("ORDER", orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode())) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //创建会员
        Member member = new Member();
        member.setName(orderSubmitParam.getName());
        member.setSex(orderSubmitParam.getSex());
        member.setPhoneNumber(orderSubmitParam.getTelephone());
        member.setIdCard(orderSubmitParam.getIdCard());
        member = memberService.addIfNotExist(member);
        //提交预约信息
        Order order = new Order();
        order.setOrderDate(orderSubmitParam.getOrderDate());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        order.setMemberId(member.getId());
        order = orderService.add(order);

        log.info("[预约服务-预约信息返回]data:{}", order);
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[预约服务-预约成功信息获取]id:{}", id);
        Order order = orderService.findById(id);
        Member member = memberService.findById(order.getMemberId());
        Setmeal checkMeal = checkMealService.findByMealId(order.getSetmealId());

        //封装返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("member", member.getName());
        map.put("setmeal", checkMeal.getName());
        map.put("orderDate", new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        map.put("orderType", order.getOrderType());

        log.info("[预约服务-预约成功信息获取]data:{}", map);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, map);
    }
}
